﻿Public Class ModifyAddress
    Const HideChar As String = "********************************************************"
    Dim SaveClose As Boolean = True
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub
    Public Sub New(ByVal mSaveClose As Boolean)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        SaveClose = mSaveClose
    End Sub
    Dim jm As New DES, isJM As Boolean = True
    Private Const jmCode As String = "sJ235689"

    Private Sub ModifyAddress_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
        If isJM = False Then
            If Not (String.IsNullOrEmpty(Pwd.Text)) Then
                Dim str As String = Pwd.Text
                Pwd.Text = jm.Encrypt(str, jmCode)
                isJM = True
            End If
        End If
    End Sub
    Private Sub ModifyAddress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataBaseIP.Text = SaveData.DataBaseIP
        If SaveData.DataBasePort > 0 Then
            Port.Text = SaveData.DataBasePort
        Else
            Port.Text = "1433"
        End If
        DataBase.Text = SaveData.DataBase
        UserID.Text = SaveData.DataBaseUserName
        Pwd.Text = SaveData.DataBasePassword
        Btn_Save.Enabled = False
        ChangeButtonColor(Me.Btn_Save)
        ChangeButtonColor(Me.ConnectTest)
        AddHandler DataBase.TextChanged, AddressOf TextChange
        AddHandler DataBaseIP.TextChanged, AddressOf TextChange
        AddHandler Port.TextChanged, AddressOf TextChange
        AddHandler UserID.TextChanged, AddressOf TextChange
        AddHandler Pwd.TextChanged, AddressOf TextChange
    End Sub
    Private Sub TextChange(ByVal sender As Object, ByVal e As EventArgs)
        Btn_Save.Enabled = False
        If DirectCast(sender, TextBox).Name = "Port" Then
            If DirectCast(sender, TextBox).Text = "" Or DirectCast(sender, TextBox).Text = "0" Then DirectCast(sender, TextBox).Text = "1433"
        End If
    End Sub
    Private Sub ConnectTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectTest.Click
        Try
            Dim sql As New SqlDataBase
            sql.ConnectDataBase(DataBaseIP.Text, Port.Text, UserID.Text, jm.Decrypt(Pwd.Text, jmCode), DataBase.Text)
            sql.Close()
            sql.Dispose()
            Btn_Save.Enabled = True
            MsgBox("链接成功,地址可用！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "链接失败")
        End Try
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click
        Try
            With SaveData
                .DataBase = DataBase.Text
                .DataBaseIP = DataBaseIP.Text
                .DataBasePassword = Pwd.Text
                .DataBasePort = Port.Text
                .DataBaseUserName = UserID.Text
            End With
            Form1.SaveSetting()
            MsgBox("设置保存成功！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
            If SaveClose = True Then Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "错误")
        End Try
    End Sub

    Private Sub Pwd_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Pwd.Enter
        If Not (String.IsNullOrEmpty(Pwd.Text)) Then
            Dim str As String = Pwd.Text
            Pwd.Text = jm.Decrypt(str, jmCode)
            isJM = False
        End If
    End Sub

    Private Sub Pwd_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Pwd.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 122 Then
                Me.Text = "不支持字符" & e.KeyChar & "的输入！" : e.Handled = True
            End If
        End If
    End Sub

    Private Sub Pwd_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pwd.TextChanged

    End Sub

    Private Sub Pwd_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Pwd.KeyUp

    End Sub

    Private Sub Pwd_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Pwd.Leave
        If isJM = False Then
            If Not (String.IsNullOrEmpty(Pwd.Text)) Then
                Dim str As String = Pwd.Text
                Pwd.Text = jm.Encrypt(str, jmCode)
                isJM = True
            End If
        End If
    End Sub

    Private Sub ModifyAddress_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        If isJM = False Then
            If Not (String.IsNullOrEmpty(Pwd.Text)) Then
                Dim str As String = Pwd.Text
                Pwd.Text = jm.Encrypt(str, jmCode)
                isJM = True
            End If
        End If
    End Sub
End Class